package ace.module.oss.test.core.listener;

import ace.cmp.json.api.JsonService;
import ace.module.common.api.constant.CommonConstants;
import ace.module.oss.core.impl.dao.entity.OssSupplier;
import ace.module.oss.core.impl.dao.repository.OssSupplierRepository;
import java.nio.file.Files;
import java.nio.file.Paths;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.dromara.x.file.storage.core.FileStorageProperties;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * module init event listener
 *
 * @author Caspar
 */
@Order(2)
@Slf4j
@RequiredArgsConstructor
@Component
public class OssInitTestListener implements ApplicationListener<ApplicationStartedEvent> {

  private final OssSupplierRepository ossSupplierRepository;
  private final JsonService jsonService;

  @SneakyThrows
  @Override
  public void onApplicationEvent(ApplicationStartedEvent event) {
    // 初始化测试阿里云oss
//    OssSupplier ossSupplier = this.ossSupplierRepository.getById(CommonConstants.DEFAULT_APP_ID);
//    String configFileJsonString =
//        Files.readString(
//            Paths.get("d:/tmp/ace/test/ace-module-oss-core-starter/aliyun_config.json"));
//    FileStorageProperties.AliyunOssConfig config =
//        jsonService.toObject(configFileJsonString, FileStorageProperties.AliyunOssConfig.class);
//    ossSupplier.setConfig(jsonService.toJson(config));
//    ossSupplierRepository.updateById(ossSupplier);
  }
}
